package rensoft.jre.io;

import static cs.java.lang.Lang.exception;
import static cs.java.lang.Lang.string;

abstract class PathImpl implements Path {

  public static Path create(String path) {
    java.io.File file = new java.io.File(path);
    if (file.isFile()) return new FileImpl(file);
    return new DirImpl(file);
  }

  protected java.io.File jreFile;

  public Dir asDir() {
    if (isDir()) return (Dir) this;
    throw exception("Not a direcotry");
  }

  public File asFile() {
    if (!isDir()) return (File) this;
    throw exception("Not a file");
  }

  public void clear() {
    delete();
    create();
  }

  public boolean exists() {
    return jreFile.exists();
  }

  public String getFullPath() {
    return jreFile.getAbsolutePath();
  }

  public String getName() {
    return jreFile.getName();
  }

  public Dir getParent() {
    return new DirImpl(jreFile.getParentFile());
  }

  public String getPath() {
    return jreFile.getPath();
  }

  public boolean isFile() {
    return !isDir();
  }

  public void setPath(String parent, String... path) {
    if (path.length == 0)
      jreFile = new java.io.File(parent);
    else if (parent == null)
      jreFile = new java.io.File(string(SEPARATOR, path));
    else jreFile = new java.io.File(parent, string(SEPARATOR, path));
    check();
  }

  public java.io.File toJRE() {
    return jreFile;
  }

  @Override
  public String toString() {
    return getPath();
  }

  protected abstract void check();
}
